﻿using System.Modules.SharePoint.Constants;
using System.Web.UI.WebControls;

namespace System.Modules.SharePoint.Contracts.DTO
{
    /// <summary>
    /// Use to Populate Field Ref
    /// </summary>
    public class SPQueryFieldRef
    {
        #region Properties
        /// <summary>
        /// SP Field Name
        /// </summary>
        public string FieldName { get; set; }
        /// <summary>
        /// Is Lookup Id
        /// </summary>
        public bool? IsLookupId { get; set; }

        /// <summary>
        /// Sort Direction Ascending | Descending
        /// </summary>
        public SortDirection? Direction { get; set; }
        #endregion

        #region Constructor
        /// <summary>
        /// Default Constructor
        /// </summary>
        public SPQueryFieldRef() { }

        /// <summary>
        /// Initiate a SP Query Field Ref object
        /// </summary>
        public SPQueryFieldRef(string fieldName)
        {
            FieldName = fieldName;
        }

        /// <summary>
        /// Initiate a SP Query Field Ref object
        /// </summary>
        public SPQueryFieldRef(string fieldName, bool isLookupId)
        {
            FieldName = fieldName;
            IsLookupId = isLookupId;
        }
        #endregion

        #region Implicit Operators
        /// <summary>
        /// Implicit conversion operator to string
        /// </summary>
        /// <param name="value">The SP Query Value</param>
        /// <returns>A string representation of the SP Query Field Ref object</returns>
        public static implicit operator string(SPQueryFieldRef value)
        {
            return value.ToString();
        }
        #endregion

        #region Override
        /// <summary>
        /// Overrides To String
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string value;
            if (IsLookupId.HasValue)
            {
                value = string.Format(SPQueryTemplates.FIELD_REF_LOOKUP, FieldName, IsLookupId.Value);
            }
            else if(Direction.HasValue)
            {
                value = string.Format(SPQueryTemplates.FIELD_REF_ORDER, FieldName, (Direction == SortDirection.Ascending) ? bool.TrueString : bool.FalseString);
            }
            else
            {
                value = string.Format(SPQueryTemplates.FIELD_REF, FieldName);
            }
            return value;
        }
        #endregion
    }
}
